// https://redux.js.org/introduction/getting-started
import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
import type { CommonStoreType } from "@/typings/storeType";
import config from "@/config";

const commonSlice = createSlice({
  name: "common",
  initialState: (): CommonStoreType => {
    return {
      platformBaseUrl: config.platformBaseUrl,
      proxyUrl: config.proxyUrl,
      websocketUrl: config.websocketUrl,
      networkState: false,
      networkType: "",
      lastNetworkChangeTime: "",
      dict: {} as CommonStoreType["dict"]
    };
  },
  reducers: {
    setPlatformBaseUrl: (state, action: PayloadAction<string>) => {
      state.platformBaseUrl = action.payload;
    },
    setProxyUrl: (state, action: PayloadAction<string>) => {
      state.proxyUrl = action.payload;
    },
    setWebsocketUrl: (state, action: PayloadAction<string>) => {
      state.websocketUrl = action.payload;
    },
    setNetworkState: (state, action: PayloadAction<boolean>) => {
      state.networkState = action.payload;
    },
    setNetworkType: (state, action: PayloadAction<string>) => {
      state.networkType = action.payload;
    },
    setLastNetworkChangeTime: (state, action: PayloadAction<string>) => {
      state.lastNetworkChangeTime = action.payload;
    },
    setDict: (state, action: PayloadAction<CommonStoreType["dict"]>) => {
      state.dict = action.payload;
    }
  }
});

export const {
  setPlatformBaseUrl,
  setProxyUrl,
  setNetworkState,
  setNetworkType,
  setLastNetworkChangeTime,
  setDict,
  setWebsocketUrl
} = commonSlice.actions;

export { commonSlice };
